package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/travel")
public class TravelController {
    
    @Autowired
    private ITravelService travelService;


    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        qo.setOrderBy("t.createTime asc");
        qo.setIsPublic(true);
        if (qo.getState()==null) {
            qo.setState(Travel.STATE_WAIT);
        }
        PageInfo<Travel> pageInfo = travelService.list(qo);
        model.addAttribute("pageInfo",pageInfo);


        return "travel/list";
    }
    @RequestMapping("/releaseList")
    public String releaseList(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        qo.setOrderBy("t.releaseTime desc");
        qo.setIsPublic(true);
        qo.setState(Travel.STATE_RELEASE);
        PageInfo<Travel> pageInfo = travelService.list(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "travel/releaseList";
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public JSONResult updateStateById(Travel travel){
        travelService.updateStateById(travel);
        return new JSONResult();
    }

    @RequestMapping("/getContent")
    @ResponseBody
    public TravelContent getContentById(Long id){

        return travelService.getContentById(id);
    }
}
